<?php

namespace Apps\AppApi\Method\BeautyParlor;

use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\BeautyParlorService;
use Apps\Common\Models\BeautyParlor;


/**
 * 服务详情
 * @package Apps\AppApi\Method\BeautyParlor
 */
class ServiceInfo extends BaseMethod
{
    public function run()
    {
        $id = intval($this->getDataItem('id',0));

        if(0 < $id) {
            /**
             * 查询服务信息
             */
            $service = BeautyParlorService::query()
                ->columns([
                    'service_id as id',
                    'service_cover as cover',
                    'service_name as name',
                    'service_price as price',
                    'service_unit as unit',
                	'service_state as state',
                    'service_description as description',
                    'service_intro as intro',
                    'beauty_parlor_id as bp_id',
                	'service_is_sell as is_sell'
                	//'service_state as state'
                ])
                ->where('service_id=:id:')
                ->bind(['id' => $id])
                ->execute()->getFirst();
        }
        $data = null;
//        $beauty_parlor_info=BeautyParlor::query()->columns(['bp_address as address','bp_tel as tel','AsText(bp_coordinate) as bp_coordinate'])->where("bp_id = ".$service->bp_id)->execute()->getFirst();
//        $coordinate=str_replace("POINT(",'',$beauty_parlor_info->bp_coordinate);
//        $coordinate=str_replace(")",'',$coordinate);
//        $coordinate=explode(" ",$coordinate);
        if($service) {
            $data = [
                'id' => $service->id,
                'cover' => PicUrl::BeautyParlorCover($service->cover, $this->getDi()),
                'name' => $service->name,
                'price' => $service->price,
                'description' => $service->description,
                'intro' => \Apps\Common\Libs\BeautyParlor::formatServiceIntro($service->intro, $this->getDi(), true),
                'service_state'=>$service->state,
                'is_sell'=>$service->is_sell,
                'unit' => $service->unit,
                /* 'address'=>$beauty_parlor_info->address,
                'latitude'=>$coordinate[0],
                'longitude'=>$coordinate[1],
                'tel'=>$beauty_parlor_info->tel */
            ];
            if($service->price > 0) {
                $data['price_show'] = $service->price . '元' . ($service->unit ? '/' . $service->unit : '');
            }
            $data['beauty_parlor'] = $this->getBeautyParlor($service->bp_id);
        }
        $this->setResult($data);
    }

    private function getBeautyParlor($id)
    {
        $info = BeautyParlor::query()
            ->columns([
                'bp_id as id',
                'bp_name as name',
                'bp_medal as medal',
            	'bp_address as address',
            	'bp_tel as tel',
            	'AsText(bp_coordinate) as bp_coordinate'
            ])
            ->where('bp_id=:id:')
            ->bind(['id' => $id])
            ->execute()->getFirst();
        $coordinate=str_replace("POINT(",'',$info->bp_coordinate);
        $coordinate=str_replace(")",'',$coordinate);
        $coordinate=explode(" ",$coordinate);
        if($info) {
            return [
                'id' => $info->id,
                'name' => $info->name,
                'medal_refund' => \Apps\Common\Libs\BeautyParlor::getMedalRefund($info->medal),
                'medal_verify' => \Apps\Common\Libs\BeautyParlor::getMedalVerify($info->medal),
                'latitude'=>$coordinate[0],
                'longitude'=>$coordinate[1],
                'tel'=>$info->tel,
                'address'=>$info->address
            ];
        }
        return null;
    }

}